package com.toilet.easyvideofx.api;

import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.HttpService;
import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;

/**
 * 回声接口
 * @description: 服务
 */
public class GreetService implements HttpService {

    @Override
    public void routing(HttpRules rules) {
        rules.get("/", this::getDefaultMessageHandler)
                .get("/{name}", this::getMessageHandler)
                .get("/greeting", this::updateGreetingHandler);
    }


    /**
     * 默认的接口
     * @param request
     * @param response
     */
    private void getDefaultMessageHandler(ServerRequest request, ServerResponse response) {
        sendResponse(response, "World");
        System.out.println("接受请求");
    }

    /**
     * 若是get请求，且是有name参数，那么会调用此方法
     * @param request
     * @param response
     */
    private void getMessageHandler(ServerRequest request, ServerResponse response) {
        sendResponse(response, "newGreeting");
    }

    /**
     * 更新接口
     * @param request
     * @param response
     */
    private void updateGreetingHandler(ServerRequest request, ServerResponse response) {
        String newGreeting = "request.content().get().toString()";
        sendResponse(response, newGreeting);
    }

    private void sendResponse(ServerResponse response, String newGreeting) {
        response.send(newGreeting);
    }
}
